#include "DiscreteDisplay.h"
#include "DisplayController.h"
#include "SleepTimer.h"
#include <QWidget>
#include "MainWindow.h"


DisplayController::DisplayController()
{
	createWidgets();
}

void DisplayController::createWidgets()
{
	_pMainWindow = new MainWindow(this);
}

void DisplayController::showDisplay()
{
	_pMainWindow->show/*Maximized*/();
	calculatePositions();
}

void DisplayController::addDisplayLeft(LMONDisplay* pDisplay)
{
	_pMainWindow->getLeftColumn()->addDisplay(pDisplay);
}

void DisplayController::addDisplayRight(LMONDisplay* pDisplay)
{
	_pMainWindow->getRightColumn()->addDisplay(pDisplay);
}

void DisplayController::setTimeScale(int timeScale)
{
	if (timeScale > 0)
	{
		_timeScale = timeScale;
		_pMainWindow->getLeftColumn()->setTimeScale(_timeScale);
		_pMainWindow->getRightColumn()->setTimeScale(_timeScale);

		repaint();
	}
}

void DisplayController::calculatePositions()
{
	_pMainWindow->getLeftColumn()->calculatePositions();
	_pMainWindow->getRightColumn()->calculatePositions();
}

void DisplayController::run()
{
	SleepTimer timer;
	while(1) {
		repaint();
		timer.sleep(25);
	}
}

void DisplayController::repaint()
{
	if(0) {
		_pMainWindow->getLeftColumn()->repaint(); //synchronous
		_pMainWindow->getRightColumn()->repaint(); //synchronous
	} else {
		_pMainWindow->getLeftColumn()->update();
		_pMainWindow->getRightColumn()->update();
	}
	
}
